package de.nordakademie.wpk.share.core.data.datastorage.share;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.LinkedHashMap;
import java.util.List;

import de.nordakademie.wpk.share.core.data.Share;
import de.nordakademie.wpk.share.core.data.webdata.StockData;
import de.nordakademie.wpk.share.core.data.webdata.webservicex.StockQuoteObj;
import de.nordakademie.wpk.share.core.data.webdata.webservicex.StockQuoteObj.Stock;

/**
 * This container inherits all shares used in the application with the current
 * data.
 * 
 * @author Christian Baumhof
 */
public class ShareContainer {

	private List<Share> shares;

	public ShareContainer() {
		initShares();
	}

	public List<Share> getShares() {
		return shares;
	}

	public Share getShareBySymbol(String symbol) {
		for (Share share : shares) {
			if (share.getSymbol().equals(symbol)) {
				return share;
			}
		}
		return null;
	}

	/**
	 * Refreshed current quotes
	 */
	public void refreshCurrentData() {
		ArrayList<String> symbols = new ArrayList<String>();
		for (Share share : shares) {
			symbols.add(share.getSymbol());
		}
		StockQuoteObj stockQuote = StockData.getWebserviceData(symbols);
		for (Stock stock : stockQuote.getStock()) {
			for (Share share : shares) {
				if (share.getSymbol().equals(stock.getSymbol().toLowerCase())) {
					share.setAnnRange(stock.getAnnRange());
					share.setPercentageChange(stock.getPercentageChange());
					share.setQuotation(stock.getLast());
					share.setPreviousClose(stock.getPreviousClose());
				}
			}
		}

	}

	/**
	 * Gets old quotes (last 30 days)
	 */
	public void refreshHistoricData() {
		GregorianCalendar today = new GregorianCalendar();
		int day = today.get(Calendar.DAY_OF_MONTH);
		int month = today.get(Calendar.MONTH);
		int year = today.get(Calendar.YEAR);
		for (Share share : shares) {
			String symbol = share.getSymbol();
			List<Stock> stocks = StockData.getHistoricYahooData(symbol, day,
					month, year);
			LinkedHashMap<GregorianCalendar, BigDecimal> quotes = new LinkedHashMap<GregorianCalendar, BigDecimal>();
			for (Stock stock : stocks) {
				quotes.put(getGregorianCalendar(stock.getDate()),
						stock.getLast());
			}
			share.setHistoricQuotes(quotes);
		}
	}

	/**
	 * Returns old data (last 30 days) for a specific share
	 * 
	 * @param symbol
	 * @return LinkedHashMap
	 */
	public LinkedHashMap<GregorianCalendar, BigDecimal> getHistoricData(
			String symbol) {
		for (Share share : shares) {
			if (share.getSymbol().equals(symbol)) {
				return share.getHistoricQuotes();
			}
		}
		return new LinkedHashMap<GregorianCalendar, BigDecimal>();
	}

	private GregorianCalendar getGregorianCalendar(String date) {
		int year = Integer.valueOf(date.substring(0, 4));
		int month = Integer.valueOf(date.substring(5, 7)) - 1;
		int day = Integer.valueOf(date.substring(8, 10));
		return new GregorianCalendar(year, month, day);
	}

	private void initShares() {
		shares = SupportedShares.getSupportedShares();
	}
}
